/**
 * @date 2006-3-9 13:32:13
 *
 */
package org.nanhill.support.spring.mvc;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

/**
 * e.g: http://myapp/method1.html --> the mapping controller's method, named
 * method1. You can use this method name resolver with MutiActionController
 * together. Like bellow : http://myapp/m*.html --> your MutiActionController
 * --> the mapping controller's method, named m*.
 * 
 * @author alin [xalinx at gmail dot com]
 * 
 */
public class PathMethodNameResolver implements MethodNameResolver {
	public String getHandlerMethodName(HttpServletRequest request)
			throws NoSuchRequestHandlingMethodException {
		String uri = request.getRequestURI();
		//e.g: uri = http://myapp/my.html?id=001
		int begin = uri.lastIndexOf('/');
		if (-1 == begin) {
			begin = 0;
		} else {
			begin++;
		}
		int end = uri.indexOf("?");
		if (-1 == end) {
			end = uri.length();
		}
		// e.g: filename = my.html
		String fileName = uri.substring(begin, end);
		end = fileName.indexOf(".");
		if (-1 != end) {
			// e.g: filename = my
			fileName = fileName.substring(0, end);
		}
		// e.g: return "my"
		return fileName;
	}
}
